package com.ly.consumer.server.impl;

import com.ly.consumer.common.constants.MqConstants;
import com.ly.consumer.common.mq.DelayMessagrProcessor;
import com.ly.consumer.common.utils.MultiDelayMessage;
import com.ly.consumer.server.IOrderServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 用于测试的
 *
 * @author 23087
 */
@Service
@Slf4j
public class OrderServerImpl implements IOrderServer {


    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 测试延迟消息队列，测试
     */
    @Override
    public void testDelayMethod() {
        // todo 前置操作 ...
        // 1.订单 id
        Long orderId = 1L;

        // 延迟检测订单状态消息
        try {
            // 订单状态检测时间
            MultiDelayMessage<Long> msg = MultiDelayMessage.of(orderId,10000L,10000L,10000L,15000L,15000L,20000L,20000L,30000L);
            rabbitTemplate.convertAndSend(
                    MqConstants.DELAY_EXCHANGE,MqConstants.DELAY_ORDER_ROUTING_KEY,msg,
                    new DelayMessagrProcessor(msg.removeNextDelay().intValue())
            );
        }catch (AmqpException e){
            log.error("发送延迟消息异常",e);
        }



    }

    /**
     * 取消订单-恢复库存
     * @param orderId
     */
    @Override
    @Transactional
    public void cancelOrder(Long orderId) {
        // todo 数据库操作-取消订单

        // todo 数据库操作-恢复库存

    }
}
